﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program16GDI
{
    /// <summary>
    /// 验证码
    /// </summary>
  public   class ValidetaCode
    {

    
            //创建验证码
            public static string CreateVCode(int length)
            {
                int[] randMembers = new int[length];
                int[] validateNums = new int[length];
                string validateNumberStr = "";

                //生成起始序列值  unchecked表示不检测溢出，计算出一个相对大的随机数，不需要准确结果
                int seekSeek = unchecked((int)DateTime.Now.Ticks);
                Random seekRand = new Random(seekSeek);
                //根据用户输入的位数去生成随机数
                int beginSeek = seekRand.Next(0, Int32.MaxValue - length * 10000);

                int[] seeks = new int[length];
                for (int i = 0; i < length; i++)
                {
                    beginSeek += 10000;
                    seeks[i] = beginSeek;
                }

                //生成随机数字
                for (int i = 0; i < length; i++)
                {
                    Random rnd = new Random(seeks[i]);
                    int pownum = 1 * (int)Math.Pow(10, length);
                    randMembers[i] = rnd.Next(pownum, Int32.MaxValue);
                }

                //抽取随机数
                for (int i = 0; i < length; i++)
                {
                    string numStr = randMembers[i].ToString();
                    int numLength = numStr.Length;
                    Random rnd = new Random();
                    int numPosition = rnd.Next(0, numLength - 1);
                    validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
                }

                //把数组转化为最后得到的字符串
                foreach (var item in validateNums)
                    validateNumberStr += item.ToString();

                return validateNumberStr;
            }

            //创建验证码的图片
            public static byte[] CreateVcodePicture(string vCode)
            {
                return null;
            }
        }

    }

